package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PhotoRecord;
import com.ruoyi.system.service.IPhotoRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.conf.MinioConfig;

/**
 * 拍照记录Controller
 * 
 * @author xyz
 * @date 2025-05-30
 */
@RestController
@RequestMapping("/system/picRecord")
public class PhotoRecordController extends BaseController
{
    @Autowired
    private IPhotoRecordService photoRecordService;

    @Autowired
    private MinioConfig minioConfig;

    // 新的域名
//    private static final String NEW_DOMAIN = "https://app1.xyzttkx.site";
//    private static final String NEW_DOMAIN = "https://app1.shuoshuohappy.site";
    private static final String NEW_DOMAIN = "https://app1.shuoshuohappy.site";

    /**
     * 转换图片URL
     */
    private void transformPhotoUrl(PhotoRecord record) {
        if (record != null && record.getPhotoUrl() != null) {
            String oldUrl = record.getPhotoUrl();
            String newUrl = oldUrl.replace(minioConfig.getEndpoint(), NEW_DOMAIN);
            record.setPhotoUrl(newUrl);
        }
    }

    /**
     * 转换图片URL列表
     */
    private void transformPhotoUrls(List<PhotoRecord> records) {
        if (records != null) {
            for (PhotoRecord record : records) {
                transformPhotoUrl(record);
            }
        }
    }

    /**
     * 查询拍照记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:picRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(PhotoRecord photoRecord)
    {
        startPage();
        List<PhotoRecord> list = photoRecordService.selectPhotoRecordList(photoRecord);
        // 转换URL
        transformPhotoUrls(list);
        return getDataTable(list);
    }

    /**
     * 导出拍照记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:picRecord:export')")
    @Log(title = "拍照记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PhotoRecord photoRecord)
    {
        List<PhotoRecord> list = photoRecordService.selectPhotoRecordList(photoRecord);
        // 转换URL
        transformPhotoUrls(list);
        ExcelUtil<PhotoRecord> util = new ExcelUtil<PhotoRecord>(PhotoRecord.class);
        util.exportExcel(response, list, "拍照记录数据");
    }

    /**
     * 获取拍照记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:picRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        PhotoRecord record = photoRecordService.selectPhotoRecordById(id);
        // 转换URL
        transformPhotoUrl(record);
        return success(record);
    }

    /**
     * 新增拍照记录
     */
    @PreAuthorize("@ss.hasPermi('system:picRecord:add')")
    @Log(title = "拍照记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PhotoRecord photoRecord)
    {
        return toAjax(photoRecordService.insertPhotoRecord(photoRecord));
    }

    /**
     * 修改拍照记录
     */
    @PreAuthorize("@ss.hasPermi('system:picRecord:edit')")
    @Log(title = "拍照记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PhotoRecord photoRecord)
    {
        return toAjax(photoRecordService.updatePhotoRecord(photoRecord));
    }

    /**
     * 删除拍照记录
     */
    @PreAuthorize("@ss.hasPermi('system:picRecord:remove')")
    @Log(title = "拍照记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(photoRecordService.deletePhotoRecordByIds(ids));
    }
}
